//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.agency;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
import org.jpgolay.gui.fixed.BasicMenu;
import org.jpgolay.gui.iconBar.*;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.util.BrowserControl;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.agency:
//            AgencyGUI, Agency, AgencyPrint
public class AgencyController extends AgencyGUI {

    public AgencyController(Parameter param) {
        create = false;
        write = false;
        this.param = param;
        crud = new ObjectCRUD();
        initMenu();
        initIconBar();
        initField();
        initAgencyList();
        initLaw();
    }

    private void initLaw() {
        if (param.getAccessLaw().length() >= 9 && param.getAccessLaw().charAt(9) == '1') {
            write = true;
        }
    }

    private void initMenu() {
        getMenuPanel().getExit().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionExit();
            }
        });
        getMenuPanel().getDelete().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionDelete();
            }
        });
        getMenuPanel().getHelp().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionHelp();
            }
        });
        getMenuPanel().getModify().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionModify();
            }
        });
        getMenuPanel().getNew().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionNew();
            }
        });
        getMenuPanel().getOpen().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionevent) {
            }
        });
        getMenuPanel().getPrint().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionPrint();
            }
        });
        getMenuPanel().getSave().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSave();
            }
        });
    }

    private void initIconBar() {
        getIconBarPanel().addAnswerListener(new AnswerListener() {
            public void create(AnswerEvent e) {
                actionNew();
            }

            public void open(AnswerEvent answerevent) {
            }

            public void modify(AnswerEvent e) {
                actionModify();
            }

            public void save(AnswerEvent e) {
                actionSave();
            }

            public void remove(AnswerEvent e) {
                actionDelete();
            }

            public void print(AnswerEvent e) {
                actionPrint();
            }

            public void help(AnswerEvent e) {
                actionHelp();
            }
        });
    }

    private void initField() {
        cleanField();
        setEnabledField(false);
        getIconBarPanel().setEnabledCreate(false);
        getIconBarPanel().setEnabledSave(false);
        getIconBarPanel().setEnabledModify(false);
        getIconBarPanel().setEnabledRemove(false);
        getIconBarPanel().setEnabledPrint(false);
        getIconBarPanel().setEnabledOpen(false);
        getMenuPanel().getNew().setEnabled(false);
        getMenuPanel().getSave().setEnabled(false);
        getMenuPanel().getModify().setEnabled(false);
        getMenuPanel().getDelete().setEnabled(false);
        getMenuPanel().getPrint().setEnabled(false);
        getMenuPanel().getOpen().setEnabled(false);
        create = false;
    }

    private void initAgencyList() {
        java.util.Collection c = crud.readAll(null, 21);
        if (c != null) {
            Agency agency = null;
            agencyList = new ArrayList(c);
            Iterator it = agencyList.iterator();
            String list[] = new String[agencyList.size()];
            for (int i = 0; it.hasNext(); i++) {
                agency = (Agency) it.next();
                list[i] = agency.getRaisonSociale();
            }

            setAgencyListValue(list);
            getAgencyListComboBox().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    actionLoadAgency(getAgencyListComboBox().getSelectedIndex());
                }
            });
        } else {
            setAgencyListValue(new String[]{
                        "aucune"
                    });
        }
    }

    private void actionExit() {
        setVisible(false);
    }

    private void actionDelete() {
        int reponse = JOptionPane.showConfirmDialog(null, "Etes-vous certain de vouloir suprimer cette Agence ?", "Confirmation", 1, 2, null);
        switch (reponse) {
            case 0: // '\0'
                actionDeleteAgency();
            // fall through

            case -1:
            case 1: // '\001'
            case 2: // '\002'
            default:
                return;
        }
    }

    private void actionHelp() {
        BrowserControl.displayURL("www.media-4-u.net");
    }

    private void actionModify() {
        setEnabledField(true);
        getIconBarPanel().setEnabledCreate(false);
        getIconBarPanel().setEnabledSave(true);
        getIconBarPanel().setEnabledModify(false);
        getIconBarPanel().setEnabledRemove(true);
        getMenuPanel().getNew().setEnabled(false);
        getMenuPanel().getSave().setEnabled(true);
        getMenuPanel().getModify().setEnabled(false);
        getMenuPanel().getDelete().setEnabled(true);
    }

    private void actionNew() {
        initField();
        setEnabledField(true);
        getIconBarPanel().setEnabledCreate(false);
        getIconBarPanel().setEnabledSave(true);
        getMenuPanel().getNew().setEnabled(false);
        getMenuPanel().getSave().setEnabled(true);
        create = true;
    }

    private void actionPrint() {
        AgencyPrint prt = new AgencyPrint((Agency) agencyList.get(getAgencyListComboBox().getSelectedIndex()), true, param);
    }

    private void actionSave() {
        if (create) {
            actionCreateAgency();
        } else {
            actionUpdateAgency();
        }
    }

    private void actionLoadAgency(int index) {
        Agency agency = (Agency) agencyList.get(index);
        initField();
        setCompanyValue(agency.getRaisonSociale());
        setStatusValue(agency.getCivilite());
        setAddrLn1Value(agency.getAdresseUn());
        setAddrLn2Value(agency.getAdresseDeux());
        setCityValue(agency.getVille());
        setZipValue(agency.getCp());
        setCountryValue(agency.getPays());
        setTel1Value(agency.getTelephoneUn());
        setFax1Value(agency.getFaxUn());
        setEmailValue(agency.getEmail());
        setSiretValue(agency.getSiret());
        setNafValue(agency.getNaf());
        setGlnValue(agency.getGln());
        setNotesValue(agency.getNotes());
        setTel2Value(agency.getTelephoneDeux());
        setTel3Value(agency.getTelephoneTrois());
        setFax2Value(agency.getFaxDeux());
        setFax3Value(agency.getFaxTrois());
        setWebValue(agency.getSiteWeb());
        getIconBarPanel().setEnabledModify(write);
        getIconBarPanel().setEnabledPrint(true);
        getMenuPanel().getModify().setEnabled(write);
        getMenuPanel().getPrint().setEnabled(true);
    }

    private void actionUpdateAgency() {
        if (verifyRequirement()) {
            Agency agency = (Agency) agencyList.get(getAgencyListComboBox().getSelectedIndex());
            agency.setRaisonSociale(getCompanyValue());
            agency.setCivilite(getStatusValue());
            agency.setAdresseUn(getAddrLn1Value());
            agency.setAdresseDeux(getAddrLn2Value());
            agency.setVille(getCityValue());
            agency.setCp(getZipValue());
            agency.setPays(getCountryValue());
            agency.setTelephoneUn(getTel1Value());
            agency.setFaxUn(getFax1Value());
            agency.setEmail(getEmailValue());
            agency.setSiret(getSiretValue());
            agency.setNaf(getNafValue());
            agency.setGln(getGlnValue());
            agency.setNotes(getNotesValue());
            agency.setTelephoneDeux(getTel2Value());
            agency.setTelephoneTrois(getTel3Value());
            agency.setFaxDeux(getFax2Value());
            agency.setFaxTrois(getFax3Value());
            agency.setSiteWeb(getWebValue());
            crud.update(agency, 21);
            initField();
            initAgencyList();
        }
    }

    private void actionCreateAgency() {
    }

    private void actionDeleteAgency() {
        Agency agency = (Agency) agencyList.get(getAgencyListComboBox().getSelectedIndex());
        agency.setRaisonSociale("Agence " + (getAgencyListComboBox().getSelectedIndex() + 1));
        agency.setCivilite("");
        agency.setAdresseUn("");
        agency.setAdresseDeux("");
        agency.setVille("");
        agency.setCp("");
        agency.setPays("");
        agency.setTelephoneUn("");
        agency.setFaxUn("");
        agency.setEmail("");
        agency.setSiret("");
        agency.setNaf("");
        agency.setGln("");
        agency.setNotes("");
        agency.setTelephoneDeux("");
        agency.setTelephoneTrois("");
        agency.setFaxDeux("");
        agency.setFaxTrois("");
        agency.setSiteWeb("");
        crud.update(agency, 21);
        initField();
        initAgencyList();
    }

    private boolean verifyRequirement() {
        if (getCompanyValue() != null && getCompanyValue().length() < 1) {
            JOptionPane.showMessageDialog(null, "Raison sociale obligatoire !");
            return false;
        }
        if (getCompanyValue() == null) {
            JOptionPane.showMessageDialog(null, "Raison sociale obligatoire !");
            return false;
        }
        if (getCompanyValue() != null && getCompanyValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Raison sociale trop longue !\n(maximum 50 carat\350res)");
            return false;
        }
        if (getAddrLn1Value() != null && getAddrLn1Value().length() > 100) {
            JOptionPane.showMessageDialog(null, "Adresse ligne 1 trop longue !\n(maximum 100 carat\350res)");
            return false;
        }
        if (getAddrLn2Value() != null && getAddrLn2Value().length() > 100) {
            JOptionPane.showMessageDialog(null, "Adresse ligne 2 trop longue !\n(maximum 100 carat\350res)");
            return false;
        }
        if (getCityValue() != null && getCityValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Ville trop longue !\n(maximum 50 carat\350res)");
            return false;
        }
        if (getZipValue() != null && getZipValue().length() > 20) {
            JOptionPane.showMessageDialog(null, "Code postal trop long !\n(maximum 20 carat\350res)");
            return false;
        }
        if (getCountryValue() != null && getCountryValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Pays trop long !\n(maximum 50 carat\350res)");
            return false;
        }
        if (getTel1Value() != null && getTel1Value().length() > 20) {
            JOptionPane.showMessageDialog(null, "T\351l 1 trop long !\n(maximum 20 carat\350res)");
            return false;
        }
        if (getFax1Value() != null && getFax1Value().length() > 20) {
            JOptionPane.showMessageDialog(null, "Fax 1 trop long !\n(maximum 20 carat\350res)");
            return false;
        }
        if (getEmailValue() != null && getEmailValue().length() > 100) {
            JOptionPane.showMessageDialog(null, "Email trop long !\n(maximum 100 carat\350res)");
            return false;
        }
        if (getSiretValue() != null && getSiretValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Siret trop long !\n(maximum 50 carat\350res)");
            return false;
        }
        if (getNafValue() != null && getNafValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Naf trop long !\n(maximum 50 carat\350res)");
            return false;
        }
        if (getGlnValue() != null && getGlnValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "GLN trop long !\n(maximum 50 carat\350res)");
            return false;
        }
        if (getNotesValue() != null && getNotesValue().length() > 250) {
            JOptionPane.showMessageDialog(null, "Commentaire trop long !\n(maximum 250 carat\350res)");
            return false;
        }
        if (getTel2Value() != null && getTel2Value().length() > 20) {
            JOptionPane.showMessageDialog(null, "T\351l 2 trop long !\n(maximum 20 carat\350res)");
            return false;
        }
        if (getTel3Value() != null && getTel3Value().length() > 20) {
            JOptionPane.showMessageDialog(null, "T\351l 3 trop long !\n(maximum 20 carat\350res)");
            return false;
        }
        if (getFax2Value() != null && getFax2Value().length() > 20) {
            JOptionPane.showMessageDialog(null, "Fax 2 trop long !\n(maximum 20 carat\350res)");
            return false;
        }
        if (getFax3Value() != null && getFax3Value().length() > 20) {
            JOptionPane.showMessageDialog(null, "Fax 3 trop long !\n(maximum 20 carat\350res)");
            return false;
        }
        if (getWebValue() != null && getWebValue().length() > 100) {
            JOptionPane.showMessageDialog(null, "Site web trop long !\n(maximum 100 carat\350res)");
            return false;
        } else {
            return true;
        }
    }
    private ObjectCRUD crud;
    private Parameter param;
    private boolean create;
    private ArrayList agencyList;
    private boolean write;
}
